package gs.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

public class QuestStatPanel extends JPanel {

	AdminWindow parent;
	private ConnectionController connection;
	DefaultListModel QuestModel;
	JList QuestList;
	JScrollPane QuestScrollPane;
	JTextField nameField;
	JTextField typeField;
	JTextField rewardField;
	JTextField itemCountField;
	JTextField itemField;
	JTextField npcCountField;
	JTextField npcField;
	
	
	QuestStatPanel(ConnectionController c, AdminWindow win)
	{
		super();
		connection = c;
		parent = win;
		
		this.setLayout(new GridLayout(1,2));
		
		JPanel choicePane = new JPanel(new BorderLayout());
		JPanel statPane = new JPanel(new GridLayout(9,2));
		
		choicePane.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Wyb�r"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
		
		statPane.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Statystyki"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
		
		QuestModel = new DefaultListModel();
		
		QuestList = new JList(QuestModel);
		QuestList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        QuestList.setSelectedIndex(0);
        QuestList.setVisibleRowCount(7);
        QuestScrollPane = new JScrollPane(QuestList);
        
        MouseListener accListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent mouseEvent) {
					getQuestData();
				}
		};
		QuestList.addMouseListener(accListener);
        
        nameField = new JTextField(" ");
        nameField.setEditable(false);
        typeField = new JTextField(" ");
        typeField.setEditable(false);
        rewardField = new JTextField(" ");
        rewardField.setEditable(false);
        itemCountField = new JTextField(" ");
        itemCountField.setEditable(false);
        itemField = new JTextField(" ");
        itemField.setEditable(false);
        npcField = new JTextField(" ");
        npcField.setEditable(false);
        npcCountField = new JTextField(" ");
        npcCountField.setEditable(false);
        
        getQuestList();
        
        choicePane.add(QuestScrollPane, BorderLayout.CENTER);
        statPane.add(new JLabel("Nazwa:"));
        statPane.add(nameField);
        statPane.add(new JLabel("Typ:"));
        statPane.add(typeField);
        statPane.add(new JLabel("Nagroda:"));
        statPane.add(rewardField);
        statPane.add(new JLabel("Liczba przedmiotów:"));
        statPane.add(itemCountField);
        statPane.add(new JLabel("Przedmiot:"));
        statPane.add(itemField);
        statPane.add(new JLabel("Liczba NPC:"));
        statPane.add(npcCountField);
        statPane.add(new JLabel("NPC:"));
        statPane.add(npcField);
        
        add(choicePane);
        add(statPane);
	}
	
	void getQuestData()
	{
		ArrayList<String> a;
		String name = (String) QuestList.getSelectedValue();
		a = connection.getQuestData(name);
		nameField.setText(a.get(0));
		typeField.setText(a.get(1));
		rewardField.setText(a.get(2));
		itemCountField.setText(a.get(3));
		itemField.setText(a.get(4));
		npcCountField.setText(a.get(5));
		npcField.setText(a.get(6));
	}
	
	void getQuestList()
	{
		ArrayList<String> a;
		
 		a = connection.getQuestList();
		QuestModel.clear();
		for(String s : a)
		{
			QuestModel.addElement(s);
		}
	}

	public void refresh() {
		getQuestList();
		
	}
}
